App({
  globalData: {
    accountInfo: null,
    userInfo: null,//微信数据包括头像这些
    bindInfo: null,
    isbind: 'loading',
    openid: '',
    baseUrl: 'https://mapp.wzzd.cn/',
    schlink: '',//学校域名
    wxinfo:{}
  },
  //重新验证登录
  revalidation: function (oauth, openid) {
    wx.request({
      url: this.globalData.baseUrl + 'revalidation',
      method: "POST",
      data: {
        oauth: 'mapp',
        openid: openid,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.msg == '重新登录' || res.data.code==0) {
          return 0;
        }
        if (res.data.msg == '登录成功' || res.data.code == 1) {
          return 1;
        }
      }
    })
  },
  onLaunch: function () {
    let self = this;
    wx.setEnableDebug({
      enableDebug: false
    })
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    if (this.globalData.openid) {
      var openid = this.globalData.openid;
      if (this.revalidation('mapp', openid) == 0) {
        wx.redirectTo({
           url: '/pages/binding/binding'
        })
      }
    } else {
      var that = this;
      wx.login({
        success: function (res) {
          var code = res.code;
          if (code) {
            wx.request({
              url: getApp().globalData.baseUrl + 'checkuser',//获取微信openid信息
              method: "POST",
              data: {
                code: code,
              },
              header: {
                "content-type": "application/x-www-form-urlencoded"
              },
              success: function (res) {
                if (res.statusCode == 200) {
                  that.globalData.wxinfo = res.data.data;
                  that.globalData.openid = res.data.data.openid;
                  wx.request({
                    url: getApp().globalData.baseUrl + 'revalidation',//重新验证登录
                    method: "POST",
                    data: {
                      oauth: 'mapp',
                      openid: res.data.data.openid,
                      link: getApp().globalData.schlink
                    },
                    header: {
                      "content-type": "application/x-www-form-urlencoded"
                    },
                    success: function (result) {
                      if (result.data.code==0) {
                        wx.redirectTo({
                          url: '/pages/binding/binding'
                        });
                        return;
                      }
                      if (!self.isbindBack) {
                        if (result.data.code == 1) {
                          getApp().globalData.bindInfo = result.data.data;
                          getApp().globalData.isbind = true;
                          wx.switchTab({
                            url: '/pages/index/index'
                          })
                        } else if (result.data.code == 0) {
                          wx.redirectTo({
                            url: '/pages/binding/binding'
                          })
                        }
                      }
                      if (self.isbindBack) {
                        self.isbindBack(result.data);
                      }
                    }
                  })
                }
              }
            })
          }
        }
      });
    }
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: res => {
              this.globalData.userInfo = res.userInfo
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
})